#region Using Statements
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Runtime.Remoting;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace SkyBurner.AI
{
    public class SkyBurnerEnemyAIManager : SkyBurnerWorldObject
    {
        #region Private Attributes
        protected Collection<EnemyAIHandlerBase> m_enemyAIHandlersList = new Collection<EnemyAIHandlerBase>();
        protected EnemyAIHandlerBase m_activeEnemyAIHandler;
        protected GameTime m_startGameTime;
        protected bool m_bActive;
        protected bool m_bNoMoreEnemy;
        #endregion

        #region Properies
        public EnemyAIHandlerBase ActiveEnemyAIHandler
        {
            get { return m_activeEnemyAIHandler; }
        }

        public bool Active
        {
            get { return m_bActive; }
            set { m_bActive = value; }
        }

        public bool NoMoreEnemy
        {
            get { return m_bNoMoreEnemy; }
        }
        #endregion

        public SkyBurnerEnemyAIManager(BaseGame pGame, SkyBurnerWorld pSkyBurnerWorld)
            : base(pGame, pSkyBurnerWorld)
        {
            m_activeEnemyAIHandler = null;
            m_bNoMoreEnemy = true;
        }

        public void AddEnemyAttackWave(BaseGame pGame, string pClassHandler, string pUnitHandlerClass, XmlNode pConfigNode)
        {
            ObjectHandle objectHandle = Activator.CreateInstance(null, pClassHandler);
            EnemyAIHandlerBase enemyAIHandler = (EnemyAIHandlerBase)objectHandle.Unwrap();

            m_Game.Console.Print("Initializing EnemyUnit: " + pClassHandler);
            m_Game.Console.Print("             EnemyUnitClass: " + pUnitHandlerClass);
           
            enemyAIHandler.Initialize(pGame, pUnitHandlerClass, m_skyBurnerWorld, pConfigNode);
            m_enemyAIHandlersList.Add(enemyAIHandler);

            m_bNoMoreEnemy = false;
        }

        public void Update(GameTime gameTime, StarFighter pPlayerStarFighter, SkyBurnerWorld pWorld)
        {
            if (m_startGameTime == null)
                m_startGameTime = new GameTime(gameTime.TotalRealTime, gameTime.ElapsedRealTime, gameTime.TotalGameTime, gameTime.ElapsedGameTime);

            if (m_activeEnemyAIHandler != null)
            {
                if (m_bActive)
                    m_activeEnemyAIHandler.Update(gameTime, pPlayerStarFighter, pWorld);

                if (!m_activeEnemyAIHandler.IsAlive())
                    m_activeEnemyAIHandler = null;
            }
            else
            {
                if (m_enemyAIHandlersList.Count > 0)
                {
                    m_activeEnemyAIHandler = m_enemyAIHandlersList[0];
                    m_enemyAIHandlersList.RemoveAt(0);
                }
                else
                    m_bNoMoreEnemy = true;
            }
        }

        public override void Draw(GameTime gameTime, Camera pActiveCamera)
        {
            if (m_activeEnemyAIHandler != null)
                m_activeEnemyAIHandler.Draw(gameTime, pActiveCamera);
        }
    }
}
